//
//  CANewAddressViewController.m
//  LNH
//
//  Created by twentylx on 2018/1/29.
//  Copyright © 2018年 twentylx. All rights reserved.
//

#import "CANewAddressViewController.h"
#import "CANewAddressView.h"
#import "CAAddressPickerView.h"
#import "CACityModel.h"
#import "CAProvinceApi.h"
#import "CAAddAddressApi.h"

@interface CANewAddressViewController () {
    NSString *_pId;
    NSString *_cId;
    NSString *_aId;
    NSString *_isDefault;
}

@property (nonatomic, strong) CANewAddressView *addressView;
@property (nonatomic, strong) CAAddressPickerView *pickerView;

@end

@implementation CANewAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = RGBCOLOR(241, 241, 241);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/// 初始化数据
- (void)lm_initialDefaultsForController {
    [self loadAddress];
}

/// 绑定 vm
- (void)lm_bindViewModelForController {
    
}

/// 创建视图
- (void)lm_createViewForController {
    [self.view setNeedsUpdateConstraints];
    
    [self.view addSubview:self.addressView];
}

/// 配置导航栏
- (void)lm_configNavigationForController {
    self.navigationItem.title = @"添加收货地址";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)];
}

#pragma mark event response
- (void) saveAction {
    NSString *name = self.addressView.nameTextField.text;
    NSString *phone = self.addressView.phoneTextField.text;
    NSString *detailAddress = self.addressView.addressDetailTextView.text;
    if (self.addressView.scitchButton.on) {
        _isDefault = @"1";
    } else {
        _isDefault = @"0";
    }
    
    CAAddAddressApi *addAddressApi = [[CAAddAddressApi alloc] initWithName:name Phone:phone PId:_pId CId:_cId AId:_aId Address:detailAddress IsDefault:_isDefault];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [addAddressApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CAAddAddressApi *api = (CAAddAddressApi *) request;
        if (api.done) {
            [SVProgressHUD lm_displaySuccessWithStatus:@"地址保存成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [SVProgressHUD lm_displayErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

#pragma mark layout
- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    [_addressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        make.top.equalTo(self.view);
        make.height.mas_equalTo(350);
    }];
}

#pragma mark get & set
- (CANewAddressView *)addressView {
    if (!_addressView) {
        _addressView = [CANewAddressView new];
        _addressView.addressTextField.inputView = self.pickerView;
    }
    return _addressView;
}

-(CAAddressPickerView *) pickerView {
    if (!_pickerView) {
        _pickerView = [[CAAddressPickerView alloc] initWithFrame:(CGRect){ 0, 0, SCREEN_WIDTH, 250 }];
        
        [_pickerView.selecedCitySubject subscribeNext:^(CACityModel *cityModel) {
            [self.addressView.addressTextField setText:[NSString stringWithFormat:@"%@ %@ %@", cityModel.province, cityModel.city, cityModel.region]];
            _pId = cityModel.provinceId;
            _cId = cityModel.cityId;
            _aId = cityModel.regionId;
//            self.viewModel.addressModel.province = cityModel.province;
//            self.viewModel.addressModel.city = cityModel.city;
//            self.viewModel.addressModel.area = cityModel.region;
        }];
    }
    return _pickerView;
}

#pragma mark private method
- (void) loadAddress {
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    CAProvinceApi *provinceApi = [[CAProvinceApi alloc] init];
    [provinceApi startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        CAProvinceApi *api = (CAProvinceApi *) request;
        if (api.done) {
            self.pickerView.localArray = api.responseObject[@"result"];
            [self.pickerView getPickerData];
        } else {
            [SVProgressHUD showErrorWithStatus:api.errorMsg];
        }
    } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [SVProgressHUD lm_displayErrorWithStatus:@"请求出错"];
    }];
}

@end
